#ifndef SERIALPORTHANDLER_H
#define SERIALPORTHANDLER_H

#include <QObject>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QTimer>

class SerialPortHandler : public QObject
{
    Q_OBJECT
public:
    SerialPortHandler();
//    explicit SerialPortHandler(QObject *parent = nullptr);
    ~SerialPortHandler();
    void closePort();
    bool openPort(int type,QString portName);
    bool getConnectStatus() const;
    void setConnectStatus(bool value);

    bool checkConnectState();
signals:
    void newQStringDataReceived(const QString &data);
    void newQByteArrayDataReceived(const QByteArray data);
public slots:
    void sendData(const QString &data);
    void sendData(QByteArray &arr);
    QString sendDataSync(const QString &data);//同步方式读取
    void readTimeOut();
private slots:
//    void requestData();
    void readData();
private:
    QSerialPort serialPort;
    QTimer timer;
    int dataType;//1:HEX  2:String
    bool connectStatus = false; // 端口连接状态位
    uint16_t sendCount = 0;  // 发送计数
    uint16_t packetLoss = 5; // 丢包率
};

#endif // SERIALPORTHANDLER_H
